<script setup>
import { Edit, Delete } from '@element-plus/icons-vue'
import { ref } from 'vue'
import {
  getChannelListService,
  artDeleteChannelService
} from '@/api/article.js'
import ChannelEdit from './components/ChannelEdit.vue'

const title = ref('文章分类')
const channelList = ref([])
const loading = ref(false)
const dialog = ref(null)

const getChannelList = async () => {
  loading.value = true
  const res = await getChannelListService()
  channelList.value = res.data.data
  loading.value = false
}

getChannelList()

const onDelChannel = async (row) => {
  await ElMessageBox.confirm('你确认删除该分类信息吗？', '温馨提示', {
    type: 'warning',
    confirmButtonText: '确认',
    cancelButtonText: '取消'
  })
  await artDeleteChannelService(row)
  ElMessage({ type: 'success', message: '删除成功' })
  getChannelList()
}

const onEditChannel = (row) => {
  dialog.value.open(row)
}

const onAddChannel = () => {
  dialog.value.open({})
}

const onSuccess = () => {
  getChannelList()
}
</script>

<template>
  <page-container :title="title">
    <template #extra>
      <el-button type="primary" @click="onAddChannel({ row })"
        >添加文章</el-button
      >
    </template>
    <template #default>
      <el-table v-loading="loading" :data="channelList" style="width: 100%">
        <el-table-column type="index" label="序号" width="180" />
        <el-table-column prop="cate_name" label="分类名称" width="180" />
        <el-table-column prop="cate_alias" label="分类别名" />
        <el-table-column label="操作">
          <template #default="{ row }">
            <!-- <el-button
              size="small"
              @click="handleEdit(scope.$index, scope.row)"
            >
              Edit
            </el-button> -->
            <el-button
              plain
              type="primary"
              :icon="Edit"
              circle
              @click="onEditChannel(row)"
            ></el-button>
            <el-button
              plain
              type="danger"
              :icon="Delete"
              circle
              @click="onDelChannel(row)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </template>
  </page-container>
  <channel-edit ref="dialog" @success="onSuccess"></channel-edit>
</template>

<style scoped></style>
